<script setup lang="ts">
import { reactive, watch, ref } from 'vue'
import useCategoryStore from '@/store/modules/category'
const categoryStore = useCategoryStore()
import type {
  HasSpuResponseData,
  Records,
  SpuData,
} from '@/api/product/spu/type'
import { reqHasSPU } from '@/api/product/spu'

import SpuFrom from './spuForm.vue'
import SkuFrom from './skuForm.vue'
//场景的数据
let scene = ref<number>(0)
// 分页器默认页码
let pageNo = ref<number>(0)
// 每一页展示几条数据
let pageSize = ref<number>(3)
//存储已有的SPU的数据
let records = ref<Records>([])
//存储已有SPU总个数
let total = ref<number>(0)

let spu = ref<any>()
let sku = ref<any>()
//监听三级分类ID变化
watch(
  () => categoryStore.c3Id,
  () => {
    //务必保证有三级分类ID
    if (!categoryStore.c3Id) return
    getHasSPU()
  },
)
//此方法执行:可以获取某一个三级分类下全部的已有的SPU
const getHasSPU = async (pager = 1) => {
  //修改当前页码
  pageNo.value = pager
  const result: HasSpuResponseData = await reqHasSPU(
    pageNo.value,
    pageSize.value,
    categoryStore.c3Id,
  )
  if (result.code === 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}
//分页器下拉菜单发生变化的时候触发
const changeSize = () => {
  getHasSPU()
}

const addSpu = () => {
  scene.value = 1
  spu.value.initAddSpu(categoryStore.c3Id)
}

const addSku = (row: SpuData) => {
  scene.value = 2
  //调用子组件的方法初始化添加SKU的数据
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}

const updateSpu = (row: SpuData) => {
  scene.value = 1
  spu.value.initHasSpuData(row)
}

const changeScene = (obj: any) => {
  scene.value = obj.flag
  if (obj.params == 'update') {
    getHasSPU(pageNo.value)
  } else {
    getHasSPU()
  }
}
</script>
<template>
  <div>
    <el-card>
      <Category :scene="scene" />
    </el-card>
    <el-card style="margin: 10px 0">
      <div v-show="scene == 0">
        <el-button
          :disabled="categoryStore.c3Id ? false : true"
          type="primary"
          size="default"
          icon="Plus"
          @click="addSpu"
        >
          添加SPU
        </el-button>
        <el-table :data="records" style="margin: 10px 0" border>
          <el-table-column
            label="序号"
            type="index"
            width="100"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="spuName"
            align="center"
            label="SPU名称"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="description"
            show-overflow-tooltip
            label="SPU描述"
          ></el-table-column>
          <el-table-column align="center" label="SPU操作" width="220">
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                title="添加SKU"
                icon="Plus"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="warning"
                size="small"
                title="修改SPU"
                icon="Edit"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="info"
                size="small"
                title="查看SKU"
                icon="View"
              ></el-button>
              <el-button
                type="danger"
                size="small"
                title="删除SKU"
                icon="Delete"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[1, 3, 5, 10]"
          :background="true"
          layout=" prev, pager, next, jumper,->,total, sizes"
          :total="total"
          :page-count="3"
          @current-change="getHasSPU"
          @size-change="changeSize"
        />
      </div>

      <div>
        <SpuFrom
          ref="spu"
          @changeScene="changeScene"
          v-show="scene === 1"
        ></SpuFrom>
        <SkuFrom
          ref="sku"
          @changeScene="changeScene"
          v-show="scene === 2"
        ></SkuFrom>
      </div>
    </el-card>
  </div>
</template>
<style scoped lang="scss"></style>
